﻿using System;
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

using Gola.VN.Scb.Web.Common.Entities;

namespace Gola.VN.Scb.Web.Common.Models
{
    [PropertiesMustMatch("Password", "RePassword", ErrorMessage = Constants.Interface.PasswordMatch)]
    public class CustomerModel:BaseEntity
    {
        public int Id { get; set; }

        [Required(ErrorMessage=Constants.Interface.UserNameRequired)]
        [DisplayName(Constants.Interface.UserName)]
        public string UserName { get; set; }

        [Required(ErrorMessage = Constants.Interface.PasswordRequired)]
        [DisplayName(Constants.Interface.Password)]
        public string Password { get; set; }

        [Required(ErrorMessage = Constants.Interface.RePasswordRequired)]
        [DisplayName(Constants.Interface.RePassword)]
        public string RePassword { get; set; }

        [Required(ErrorMessage=Constants.Interface.FullNameRequired)]
        [DisplayName(Constants.Interface.FullName)]
        public string FullName { get; set; }

        [Required(ErrorMessage=Constants.Interface.AddressRequired)]
        [DisplayName(Constants.Interface.Address)]
        public string Address { get; set; }

        [Required(ErrorMessage=Constants.Interface.EmailRequired)]
        [RegularExpression(Constants.EmailFormat, ErrorMessage=Constants.Interface.EmailInvalid)]
        [DisplayName(Constants.Interface.Email)]
        public string Email { get; set; }

        [DisplayName(Constants.Interface.Logo)]
        public string Image { get; set; }

        [Required(ErrorMessage=Constants.Interface.PhoneRequired)]
        [DisplayName(Constants.Interface.Phone)]
        [RegularExpression(Constants.PhoneFormat, ErrorMessage=Constants.Interface.PhoneInvalid)]
        public string PhoneNumber { get; set; }

        [ValueEqual(true, ErrorMessage=Constants.Interface.TermsRequired)]
        public bool IsAgree { get; set; }
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class PropertiesMustMatchAttribute : ValidationAttribute
    {
        private const string _defaultErrorMessage = "";
        private readonly object _typeId = new object();

        public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty)
            : base(_defaultErrorMessage)
        {
            OriginalProperty = originalProperty;
            ConfirmProperty = confirmProperty;
        }

        public string ConfirmProperty { get; private set; }
        public string OriginalProperty { get; private set; }

        public override object TypeId
        {
            get
            {
                return _typeId;
            }
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                OriginalProperty, ConfirmProperty);
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
            object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
            return Object.Equals(originalValue, confirmValue);
        }
    }

    public sealed class ValueEqual : ValidationAttribute
    {
        private readonly object _typeId = new object();

        public ValueEqual(object compareValue)
            : base()
        {
            CompareValue = compareValue;
        }

        public object CompareValue { get; private set; }

        public override object TypeId
        {
            get
            {
                return _typeId;
            }
        }

        public override bool IsValid(object value)
        {
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
            return Object.Equals(value, CompareValue);
        }
    }
}
